//封装storage
var storage = {
    //设置localstorage
    set(key, value){
        localStorage.setItem(key, JSON.stringify(value))
        // localStorage.key = value
        // localStorage[key] = value
    },
    //获取localstorage
    get(key){
        return JSON.parse(localStorage.getItem(key))
    },
    getForIndex(index){
        return localStorage.key(index)
    },
    getKeys(){
        let items = this.getAll()
        let keys = []
        for(let index = 0; index<items.length;index++){
            keys.push(items[index].key)
        }
        return keys
    },
    getLength(){
        return localStorage.length
    },
    getSupport(){
        return (typeof (Storage) !=="undefined") ? true:false
    },
    //移除某个存在localstorage里的值
    remove(key){
        localStorage.removeItem(key)
    },
    //清除所有localstorage
    removeAll(){
        localStorage.clear()
    },
    //获取到所有localstorage里的键值对
    getAll(){
        let len = localStorage.length
        let arr = new Array()
        for(var i =0; i<len;i++){
            var getKey = localStorage.key(i)
            var getVal = localStorage.getItem(getKey)
            arr[i] = {
                'key':getKey,
                'val':getVal
            }
        }
        return arr
    }
}

export default storage
